﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using LibraryCS;

namespace GUI {
    public partial class Form1 : Form {

        List<VImage> templateImages = new List<VImage>();
        List<VImage> testSetImages = new List<VImage>();

        public Form1() {
            InitializeComponent();
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e) {
            Application.Exit();
        }

        private void openButton_Click(object sender, EventArgs e) {
            // Show open file dialog
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "TrueType Font (*.ttf)|*.ttf";
            openFileDialog.FilterIndex = 1;
            if (openFileDialog.ShowDialog() == DialogResult.OK) {
                String filePath = Path.GetFullPath(openFileDialog.FileName);

                // Createing font set
                FontSet fs = new FontSet(filePath, openFileDialog.FileName.Split('.')[0]);

                // Create image list of template
                templateImages = fs.createBitmaps();

                // Show in listView
                for (int i = 0; i < templateImages.Count; i++) {
                    templateImageList.Images.Add(templateImages[i].localImage);
                    char character = (char)templateImages[i].localImage.Tag;
                    templateListView.Items.Add("" + character, "" + character, i);
                }

                log("Opened font " + filePath);
            }
        }

        // Clear the template list
        private void clearButton_Click(object sender, EventArgs e) {
            // Empty listView
            templateImageList.Images.Clear();
            templateListView.Items.Clear();

            // Empty image list of templates
            templateImages.Clear();

            log("Cleared templates");
        }

        // Add image to testSet list
        private void openBitmapButton_Click(object sender, EventArgs e) {
            // Show open file dialog
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "Image Files(*.BMP;*.JPG;*.JPEG;*.PNG;)|*.BMP;*.JPG;*.JPEG;*.PNG;";
            openFileDialog.FilterIndex = 1;
            if (openFileDialog.ShowDialog() == DialogResult.OK) {
                String filePath = Path.GetFullPath(openFileDialog.FileName);
                
                try {
                    // Open bitmap & create VImage
                    Bitmap bmp = new Bitmap(filePath);
                    VImage openedImage = new VImage(bmp);
                    
                    // Add VImage to list
                    testSetImages.Add(openedImage);

                    // Add bitmap to listView list
                    testSetImageList.Images.Add(bmp);
                    testSetListView.Items.Add(openFileDialog.SafeFileName,
                                              openFileDialog.SafeFileName,
                                              testSetImageList.Images.Count-1);

                } catch (FileNotFoundException) {
                    log("Failed to open " + openFileDialog.FileName + "; File not found");
                }
            }
        }

        // clear testSet images
        private void clearTestSetButton_Click(object sender, EventArgs e) {
            // Empty listView
            testSetImageList.Images.Clear();
            testSetListView.Items.Clear();

            // Empty the test set image list
            testSetImages.Clear();

            log("Cleared test set");
        }

        // Num neurons field updated
        private void numNeuronsTextBox_TextChanged(object sender, EventArgs e) {
            updateNeuronSum();
        }

        // Num layers field updated
        private void numLayersTextBox_TextChanged(object sender, EventArgs e) {
            updateNeuronSum();
        }

        // Show total amount of neurons
        private void updateNeuronSum() {
            int numLayers = 0;
            int numNeuronPerLayer = 0;
            string numLayersStr = numLayersTextBox.Text;
            string numNeuronsPerLayerStr = numNeuronsTextBox.Text;

            try {
                // parse user values
                numLayers = int.Parse(numLayersStr);
                numNeuronPerLayer = int.Parse(numNeuronsPerLayerStr);

                neuronSumLabel.Text = "" + numLayers * numNeuronPerLayer;
            } catch (FormatException) {
                neuronSumLabel.Text = "" + 0;
            }
        }

        // Train the neural network
        private void trainButton_Click(object sender, EventArgs e) {
            int numLayers = 0;
            int numNeuronPerLayer = 0;
            string numLayersStr = numLayersTextBox.Text;
            string numNeuronsPerLayerStr = numNeuronsTextBox.Text;

            try {
                // parse user values
                numLayers = int.Parse(numLayersStr);
                numNeuronPerLayer = int.Parse(numNeuronsPerLayerStr);

                // Print info
                log("Training neural network");
                log("# hidden layers = " + numLayers);
                log("# neurons/layer = " + numNeuronPerLayer);

                // Start training
                //... 

                // use templateImages as input

            } catch (FormatException) { }
        }

        // Present data to the neural network
        private void presentButton_Click(object sender, EventArgs e) {
            // Print info
            bool useSelected = useSelectedCheckBox.Checked;
            log("Present data to neural network");
            log("Use only selected image = " + useSelected);

            // Use testSetImages as input
            //VImage slectedImage = testSetImages.ElementAt<VImage>(templateTreeView.SelectedImageIndex);

        }

        // Log
        public void log(string message) {
            infoTextBox.Text += message + "\n";
        }

    }
}
